var d1 = require('deviceone')
var Base = require('core/base')
var log = require('util/log')
var filter = require('util/filter')
/**
 * 创建数据绑定类
 */
var Bind = d1.Class(Base, function(id, mapping, data, filter) {
	Base.initialize.call(this, id);

	this.data = data // 存储组件数据
	this.mapping = mapping
	this._filter = filter
	// 创建绑定数据源
	this.data_source = d1.mm('do_HashData')
	this.list_source = {}
})

/**
 * 创建绑定字典
 * 
 * @returns
 */
function createMapping() {
	var list_mapping = {}
	var data_mapping = {}
	for ( var key in this.mapping) {
		var value = this.mapping[key]
		var data
		var filter
		if (typeof value === 'string') {
			data = value.split('|')[0]
			filter = value.split('|')[1]
		} else {
			data = value
		}

		var id = key.split('.')[0]
		var target = this.getUI(id)
		var type = target.getType()
		switch (type) {
		case 'do_ListView':
		case 'do_SlideView':
		case 'do_SegmentView':
		case 'do_GridView': {
			var source = d1.mm('do_ListData')
			this.list_source[key] = source
			list_mapping[key] = {
				value : value,
				data : data,
				filter : filter,
				type : type
			}
		}
			break;
		case 'do_ExpandableListView': {
			var source_array = []
			for ( var item in value) {
				var source = d1.mm('do_ListData')
				source_array.push(source)
			}
			this.list_source[key] = source_array
			list_mapping[key] = {
				value : value,
				data : data,
				filter : filter,
				type : type
			}
		}

			break;
		default:
			data_mapping[key] = {
				value : value,
				data : data,
				filter : filter,
				type : type
			}
			break;
		}
	}
	return {
		list_mapping : list_mapping,
		data_mapping : data_mapping
	}
}

/**
 * 绑定基础数据
 * 
 * @returns
 */
function bindDataMapping() {
	var mapping = {}
	for ( var key in this.data_mapping) {
		mapping[key] = this.data_mapping[key].data
	}
	// 进行数据绑定，创建数据映射
	this._root.bindData(this.data_source, mapping);
}

/**
 * 绑定列表数据
 * 
 * @returns
 */
function bindListMapping() {
	for ( var key in this.list_source) {
		var source = this.list_source[key]
		var target = this.getUI(key)
		if (source instanceof Array) {
//			expandlist提前绑定会出现问题
//			target.bindItems.apply(target, source);
		} else {
			target.bindItems(source);
		}
	}
}

// 扩展基础方法
d1.merge(Bind.fz, {
	/**
	 * 初始化数据绑定
	 */
	init : function() {
		var mapping = createMapping.call(this)
		this.data_mapping = mapping.data_mapping
		this.list_mapping = mapping.list_mapping

		bindDataMapping.call(this)
		bindListMapping.call(this)

		// 刷新数据
		this.refresh()
		// 添加绑定数据监听
		this.addBindListener()
	},
	/**
	 * 刷新数据绑定
	 */
	refresh : function() {
		// 无参数时进行全部刷新
		if (arguments.length <= 0) {
			this.refreshDataBind()
			for ( var key in this.list_mapping) {
				this.refreshListBind(key)
			}
		} else {
			// 判断需要刷新的对象
			for ( var index in arguments) {
				var key = arguments[index]
				if (key === '$') {
					// $符号表示仅刷新基础数据
					this.refreshDataBind()
				} else {
					// 负责表示刷新对应列表数据
					this.refreshListBind(key)
				}
			}
		}
	},
	/**
	 * 刷新基础数据
	 */
	refreshDataBind : function() {
		var dataSet = Object.deepClone(this.data)
		for ( var key in this.data_mapping) {
			var id = key.split('.')[0]
			var target = this.getUI(id)
			var data = eval('this.data.' + this.data_mapping[key].data)
			var filterKey = this.data_mapping[key].filter
			var value = this.filterConvert(data, filterKey)
			eval('dataSet.' + this.data_mapping[key].data + '=value')

			// 特殊对象处理
			switch (this.data_mapping[key].type + ":" + key) {
			case "do_ComboBox:" + id + ".items":
				target.tag = data
			}
		}
		this.data_source.removeAll()
		this.data_source.addData(dataSet)
		this._root.refreshData()
	},
	/**
	 * 刷新列表数据
	 */
	refreshListBind : function(key) {
		var mapping = this.list_mapping[key]
		var value = this.list_mapping[key].value
		var source = this.list_source[key]
		var target = this.getUI(key)
		var type = this.getUI(key).getType()

		switch (type) {
		case 'do_ListView':
		case 'do_SlideView':
		case 'do_SegmentView':
		case 'do_GridView': {
			// 单数据源列表数据刷新
			var data = eval('this.data.' + value)
			source.removeAll()
			source.addData(data)
			target.refreshItems();
		}
			break;
		case 'do_ExpandableListView': {
			// 多数据源列表数据刷新
			for ( var index in source) {
				var data = eval('this.data.' + value[index])
				source[index].removeAll()
				source[index].addData(data)
			}
			//TODO:expandlist由于ios与android兼容问题每次重新做bind处理
			target.bindItems.apply(target, source);
		}
			break;
		}
	},
	/**
	 * 添加数据绑定监听
	 */
	addBindListener : function() {
		for ( var key in this.data_mapping) {
			var id = key.split('.')[0]
			var prop = key.split('.')[1]
			var value = this.data_mapping[key].value
			var target = this.getUI(id)
			var self = this
			switch (target.getType() + ':' + prop) {
			case 'do_TextField:text':
				(function() {
					var _value = value
					target.on('textChanged', function() {
						eval('self.data.' + _value + '= this.text')
					})
				})()
				break;
			case 'do_TextBox:text':
				(function() {
					var _value = value
					target.on('textChanged', function() {
						eval('self.data.' + _value + '= this.text')
					})
				})()
				break;
			case 'do_ComboBox:value':
				(function() {
					var _value = value
					target.on('selectChanged', function(index) {
						var combobox_value = filter.getTypeCodeIdByIndex(
								this.tag, index)
						eval('self.data.' + _value + '= combobox_value')
					})
				})()
				break;
			default:
				break;
			}
		}
	},
	filterConvert : function(data, filterKey) {
		var filter_fun
		if (this._filter) {
			var filter_fun = this._filter[filterKey]
		}

		if (!filter_fun) {
			filter_fun = filter[filterKey]
		}

		if (filter_fun) {
			return filter_fun(data)
		} else {
			return data
		}
	},
	getSource:function(key){
		return this.list_source[key]
	}
})

// 扩展基础属性
Object.defineProperties(Bind.fz, {

});

module.exports = Bind